package engine;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

public class RechercheBooleanSimple {

    static private int nbMots = 0;
    static private BooleanSimple root;

    static public void buildQuery(String query) {
        System.out.println(query);

        Set<String> lstWords = new HashSet<String>();
        String[] lstToken = query.split(" ");
        BooleanSimple currentParent = root;
        BooleanSimple b;
        nbMots = 0;

        for (int i = 0; i < lstToken.length; i++) {

            //init de l'arbre
            if (i == 0) {
                if (lstToken[i].contains("NOT")) {
                    root = new BooleanSimpleNot(lstToken[0]);
                } else {
                    root = new BooleanSimple(lstToken[0]);
                }

                currentParent = root;
                lstWords.add(lstToken[0]);
            } else {
                if (lstToken[i].contains("OR")) {

                    //MODIFICATION DE L ARBRE
                    b = new BooleanSimple(lstToken[i]);
                    b.addChildLeft(root);
                    root = b;
                    currentParent = b;


                } else if (lstToken[i].contains("AND")) {

                    // MODIFICATION DE L ARBRE 
                    b = new BooleanSimple(lstToken[i]);
                    if (root.isLeaf()) {
                        b.addChildLeft(root);
                        root = b;
                        currentParent = b;
                    } else if (root.getClass() == BooleanSimpleNot.class) {
                        b = new BooleanSimple(lstToken[i]);
                        b.addChildLeft(root);
                        root = b;
                        currentParent = b;

                    } else {
                        currentParent.addChildRight(b);
                    }
                    currentParent = b;

                } else if (lstToken[i].contains("NOT")) {
                    b = new BooleanSimpleNot(lstToken[i]);
                    if (root.isLeaf()) {
                        b.addChildLeft(root);
                        root = b;
                        currentParent = b;
                    } else {
                        currentParent.addChildRight(b);

                    }
                    currentParent = b;

                } else {
                    currentParent.addChild(new BooleanSimple(lstToken[i]));

                    lstWords.add(lstToken[i]);

                    nbMots++;
                }

            }

        }
        System.out.println(" TREE ---  " + root);
        System.out.println("WORD IN QUERRY   " + lstWords);
        BooleanOperator.setQuery(lstWords);

    }

    static public double calculus(HashMap<String, Double> weights2) {

        BooleanSimple.setData(weights2);


        double result = root.calculus();
        System.out.println(result);
        return result;

    }
}
